package com.bsc.rtc.lib.libraries.apkinstall;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.bsc.rtc.lib.libraries.MyApplication;
import com.bsc.rtc.lib.libraries.R;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import okio.Sink;
import okio.Source;

public class ApkInstallTActivity extends AppCompatActivity {
    String tag = "ApkInstallTActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apk_install_t);
        //  new AppManager().installApk(this);
        testFile();
        Log.e(tag, "test---");
    }

    //android 10.0 适配
    private void testFile() {
        String path = MyApplication.getInstance().getApplicationContext().getFilesDir().getAbsolutePath() + "/test.txt";
        Log.i(tag, "path " +path);
        File file = new File(path);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            writeEnv(file);
            readLines(file);
        } catch (IOException e) {
            Log.e(tag, "err " + e.getLocalizedMessage());
        }

    }

    public void readLines(File file) throws IOException {
        // 1.构建 Source
        try (Source fileSource = Okio.source(file);
             // 2.构建 BufferedSource
             BufferedSource bufferedSource = Okio.buffer(fileSource)) {

            while (true) {
                // 3.按 utf8 的格式逐行读取字符
                String line = bufferedSource.readUtf8Line();
                if (line == null) break;
                Log.i(tag, "" + line);
                if (line.contains("square")) {

                }
            }

        }
    }

    public void writeEnv(File file) throws IOException {
        // 1.构建 Sink
        try (Sink fileSink = Okio.sink(file);
             // 2.构建 BufferedSink
             BufferedSink bufferedSink = Okio.buffer(fileSink)) {
            // 3.写入文本
            for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
                bufferedSink.writeUtf8(entry.getKey());
                bufferedSink.writeUtf8("fjsklajflksjdal");
                bufferedSink.writeUtf8("=");
                bufferedSink.writeUtf8(entry.getValue());
                bufferedSink.writeUtf8("\n");
            }

        }
    }

}
